package com.student_exam_system.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.student_exam_system.Util.JwtUtil;
import com.student_exam_system.pojo.Paper;
import com.student_exam_system.pojo.Result;
import com.student_exam_system.service.PaperService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author fire'ge
 */
@Controller
@RequestMapping("/paper")
public class PaperController {
        private final PaperService paperService;

    public PaperController(@Qualifier("paperServiceImpl") PaperService paperService) {
        this.paperService = paperService;
    }
    @PreAuthorize("hasAuthority('student')")
    @RequestMapping(value = "/allPaper")
        @ResponseBody
        public String list(HttpServletRequest request) {

            String token = request.getHeader("token");
            int userId = JwtUtil.getJWTUserId(token);
            List<Paper> papers = paperService.queryPaperByUserId(userId);
            System.out.println(papers);
        Result result = Result.succ(papers);
        return JSON.toJSONString(result);
        }

    @PreAuthorize("hasAuthority('teacher')")
    @RequestMapping("/selectPaperName")
    @ResponseBody
    public String selectPaperName() {
        List<Map<Integer, String>> list = paperService.selectPaperName();
        ObjectMapper objectMapper = new ObjectMapper();
        String json=null;
        try {
            json = objectMapper.writeValueAsString(list);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }
    @PreAuthorize("hasAuthority('teacher')")
    @RequestMapping("/paperDetails/{paperId}")
    @ResponseBody
    public String paperDetail(@PathVariable int paperId) {
        List<Paper> papers = paperService.showPaperDetails(paperId);
        ObjectMapper objectMapper = new ObjectMapper();
        String json=null;
        try {
            json = objectMapper.writeValueAsString(papers);
        } catch (JsonProcessingException e

        ) {
            e.printStackTrace();
        }
        return json;
    }

    @PreAuthorize("hasAuthority('teacher')")
    @RequestMapping("/newPaper/{paperName}/{teacherId}/{questionId}/{choice}/{number}")
    @ResponseBody
    public int newPaper(@PathVariable String paperName,@PathVariable int teacherId,@PathVariable List<Integer> questionId,@PathVariable int choice,@PathVariable int number) {
        int res = paperService.newPaper(paperName, teacherId, questionId, choice, number);
        //执行成功【1】
        //没执行成功【0】
        //相关数据出错【没执行-1】
        return Integer.compare(res, 0);
    }


    @PreAuthorize("hasAuthority('teacher')")
    @RequestMapping("/putInPaper/{paperId}/{studentId}/{classId}")
    @ResponseBody
    public int putInPaper(@PathVariable int paperId,@PathVariable int studentId,@PathVariable int classId){
        return paperService.putInPaper(paperId, studentId, classId);
    }
    }

